//
//  WBMTDishesController.m
//  WBMTwaimai
//
//  Created by WangBiao on 16/9/21.
//  Copyright © 2016年 lsrain. All rights reserved.
//

#import "WBMTDishesController.h"
#import "WBMTLeftList.h"
#import "WBMTLeftCell.h"
#import "WBMTRightCell.h"

@interface WBMTDishesController ()<UITableViewDataSource>

///模型数组
@property (nonatomic, strong)NSArray<WBMTLeftList *> *arrModel;

///左边的tableV
@property (nonatomic, weak) UITableView *leftTV;
///右边的tableV
@property (nonatomic, weak) UITableView *rightTV;

@end

static NSString *cellID = @"cellID";
@implementation WBMTDishesController

#pragma make - 搭建界面
- (void)setupUI{

    self.view.backgroundColor = [UIColor whiteColor];
    
    //左边的列表
    UITableView *leftTV = [[UITableView alloc]init];
    [self.view addSubview:leftTV];
    [leftTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.view);
        make.bottom.mas_equalTo(-46);
        make.width.mas_equalTo(86);
    }];
    
    //右边的菜品列表
    UITableView *rightTV = [[UITableView alloc]init];
    [self.view addSubview:rightTV];
    
    [rightTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.view);
        make.left.equalTo(leftTV.mas_right);
        make.bottom.equalTo(leftTV);
    }];
    
    
    //购物车占位视图
    UIView *showV = [[UIView alloc]init];
    showV.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:showV];
    [showV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(rightTV.mas_bottom);
    }];
    
    //数据源设置
    leftTV.dataSource = self;
    rightTV.dataSource = self;
    
    //注册自定义cell
    [leftTV registerClass:[WBMTLeftCell class] forCellReuseIdentifier:cellID];
    [rightTV registerClass:[WBMTRightCell class] forCellReuseIdentifier:cellID];
    
    //加载json
    [self loadData];
    
    //赋值
    self.leftTV = leftTV;
    self.rightTV = rightTV;
    
    //左边列表的行高
    leftTV.rowHeight = 55;
    //右边的行高
    rightTV.rowHeight = 94;
    
    //分割线样式
    leftTV.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    if (self.leftTV == tableView) {
        return 1;
    }
    else
        return self.arrModel.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    //判断不同的cell
    if (tableView == self.leftTV) {
        //给自定义cell的模型
        ((WBMTLeftCell *)cell).leftModel = self.arrModel[indexPath.row];
    }else{
    ((WBMTRightCell *)cell).rightModel = self.arrModel[indexPath.section].spus[indexPath.row];
    }
    return cell;
}

///加载数据（JSON）
- (void)loadData{
    
#warning TO-1 - 耗时操作
    NSURL *URL = [NSURL URLWithString:@"https://raw.githubusercontent.com/wangbiaoWB/WBMTwaimai/master/food.json"];
    NSData *data = [NSData dataWithContentsOfURL:URL];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    //测试写入plist文件
//    [dic writeToFile:@"/Users/wangbiao/Desktop/json.plist" atomically:YES];
    
    NSArray *arr = dic[@"data"][@"food_spu_tags"];
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];
    
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        WBMTLeftList *model = [[WBMTLeftList alloc] init];
        [model setValuesForKeysWithDictionary:obj];
        [arrM addObject:model];
    }];
    
    self.arrModel = arrM.copy;
}


@end
